<template>
	<a-form :ref="'ruleForm'" :model="formState" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
		<a-collapse ghost v-model:activeKey="activeKey">
			<a-collapse-panel key="1" header="数值配置">
				<a-row :gutter="[10, 10]">
					<a-col :span="24">
						<a-radio-group
							v-model:value="formState.dataSetConfig.dataSetType"
							optionType="button"
							buttonStyle="solid"
							:options="[
								{ label: '静态', value: 0 },
								{ label: '数据源', value: 1 }
							]"
						>
						</a-radio-group>
					</a-col>

					<a-col :span="24" v-if="formState.dataSetConfig.dataSetType == 0">
						<JsonEditor v-model="formState.data.list.value" />
					</a-col>
					<template v-else>
						<a-col :span="24">
							<SelectTree @onChange="selectTreeChange" v-model:paramsJson="formState.dataSetConfig.dataSetJson" />
						</a-col>
						<a-col :span="24">
							<a-select
								style="width: 100%"
								placeholder="请选择数据源"
								v-model:value="formState.data.list.field"
								@change="selectDataSet"
								optionLabelProp="label"
							>
								<a-select-option
									v-for="optionItem in dataOption"
									:value="optionItem"
									:key="optionItem"
									:label="optionItem"
								>
									{{ optionItem }}
								</a-select-option>
							</a-select>
						</a-col>
					</template>
				</a-row>
			</a-collapse-panel>
		</a-collapse>
	</a-form>
</template>

<script setup>
	import { ref, computed } from 'vue'
	import SelectTree from '@/views/plugin/dashboard/plugin/public/SelectTree/SelectTree.vue'
	import JsonEditor from '@/views/plugin/dashboard/plugin/public/JsonEditor/JsonEditor.vue'
	import datasetApi from '@/views/plugin/dashboard/api/datasetApi'
	import { designStore } from '@/views/plugin/dashboard/store'
	const useDesignStore = designStore()
	import { cloneDeep } from 'lodash-es'
	// -----------------------------------------------------------
	import config from '../config'
	import { reactive, defineProps, defineEmits } from 'vue'
	const emits = defineEmits(['upDataConfig'])
	const props = defineProps({
		config: {
			type: Object,
			default: () => {
				return cloneDeep(config.data)
			}
		}
	})
	const activeKey = ref(['1'])
	// 静态
	const staticData =
		props.config.dataSetConfig.dataSetType === 1 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))
	// 动态
	const dynamicData =
		props.config.dataSetConfig.dataSetType === 0 ? ref(cloneDeep(config.data)) : ref(cloneDeep(props.config.data))

	const formState = computed({
		get() {
			const dataSetConfig = props.config.dataSetConfig // 更新的状态
			// 初始值
			const dataSource = dataSetConfig.dataSetType === 1 ? dynamicData.value : staticData.value
			return {
				data: dataSource,
				dataSetConfig: dataSetConfig
			}
		},
		set(value) {
			if (value.dataSetConfig.dataSetType === 1) {
				dynamicData.value = value.data
			} else {
				staticData.value = value.data
			}
		}
	})

	// 数据源
	const dataSource = ref([])
	// 模拟接口返回下拉数据
	const dataOption = ref([])
	// 获取下拉数据
	watch(
		() => formState.value.dataSetConfig.dataSetJson,
		(newVal) => {
			if (props.config.dataSetConfig.dataSetType === 1) {
				const list = useDesignStore.dataSource || []
				let datasetId = ''
				let indicatorId = ''
				list.forEach((parent) => {
					parent.config.indicators.forEach((child) => {
						if (child.id === newVal.id) {
							datasetId = parent.id
							indicatorId = child.id
						}
					})
				})
				if (datasetId && indicatorId) {
					// 下拉数据
					datasetApi.querySelectedColumns({ datasetId, indicatorId }).then((res) => {
						dataOption.value = res || []
					})
					datasetApi.preview(newVal).then((res) => {
						dataSource.value = res || []
					})
				}
			}
		},
		{
			immediate: true,
			deep: true
		}
	)
	// 选择数据
	const selectDataSet = (val) => {
		// 模拟接口返回数据源
		try {
			const list = []
			dataSource.value.forEach((item) => {
				const url = item[val]
				if (/http/.test(url)) {
					list.push(url)
				}
			})
			formState.value.data.list.value = list
		} catch (error) {
			console.log(error, '数据格式错误')
			formState.value.data.list.value = []
		}
	}
	// 更新组件内容
	watch(
		() => formState.value,
		(newVal) => {
			formState.value = newVal
			emits('upDataConfig', { ...props.config, ...newVal })
		},
		{
			deep: true
		}
	)
	// 数据变化清空下拉
	const selectTreeChange = () => {
		formState.value.dataSetConfig.dataSetId = undefined
	}
</script>
<style scoped>
	:deep(.ant-radio-button-wrapper) {
		padding: 0px;
		width: 100%;
	}

	:deep(.ant-radio-group) {
		display: flex;
		text-align: center;
	}
</style>
